package fileDemo.mfile;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.File;
import java.util.*;

public class FileFilter {

    private static final Logger logger = Logger.getLogger(FileFilter.class);

    @Test
    public void main() {
        logger.info("main: ");
        logger.info("testFilter: ");
        final String BBOX_SETUPWIZARD_EN_PRI = "bbox_setupwizard_en_pri";
        final String BBOX_SETUPWIZARD_ZH_PRI = "bbox_setupwizard_zh_pri";
        String[] paths = null;
        File dirDev = new File("./src/fileDemo.mfile/unZip_win");
        logger.info("main: direxit:"+dirDev.exists());
        final boolean isChinese = true;
        paths = dirDev.list(new java.io.FilenameFilter() {
            @Override
            public boolean accept(File pathFile, String name) {//pathFile 为 dir name
                logger.info("accept: file:"+pathFile.getName()+" name:"+name);
                return name != null && pathFile.exists() &&
                        (isChinese ? name.contains(BBOX_SETUPWIZARD_ZH_PRI)
                                : name.contains(BBOX_SETUPWIZARD_EN_PRI));
            }
        });

        for (int i = 0; paths!=null && i < paths.length ; i++) {
            paths[i] = dirDev.getAbsolutePath()+File.separator+paths[i];
        }
        //TODO 结果 无序
        for (int i = 0;paths!=null && i < paths.length; i++) {
            logger.info("testFilter: "+i+" "+paths[i]);
        }

        //排序
        Arrays.sort(paths);
        logger.info("testFilter: over");

    }

    @Test
    public void delSomeFile(){
        delateOldFile("./src/fileDemo.mfile/unZip_win","bbox_setupwizard_zh",".png");
    }

    private static void delateOldFile(String folderPath, final String start, final String end) {

        File folder = new File(folderPath);
        if (folder.exists() && folder.isDirectory()){
            File[] files = folder.listFiles(new java.io.FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    String name = pathname.getName();

                    return name.startsWith(start) && name.endsWith(end);
                }
            });
            for (File delFile: files) {
                boolean delete = delFile.delete();
                System.out.println("delateOldFile: del oldfile :"+delete+" "+delFile.getAbsolutePath());
            }
        }

    }

}